package com.example.monitorsms.http;

import android.app.Dialog;
import android.content.Intent;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.example.monitorsms.LoginActivity;
import com.example.monitorsms.MyApp;
import com.example.monitorsms.R;
import com.example.monitorsms.utils.LogUtils;
import com.example.monitorsms.utils.SPUtils;
import com.lzy.okgo.callback.AbsCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;

import java.net.ConnectException;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.net.UnknownServiceException;

/**
 * Created by cxf on 2017/8/7.
 */

public abstract class HttpCallbackData extends AbsCallback<JsonBeanData> {
    private Dialog mLoadingDialog;

    @Override
    public JsonBeanData convertResponse(okhttp3.Response response) throws Throwable {
        return JSON.parseObject(response.body().string(), JsonBeanData.class);
    }

    @Override
    public void onSuccess(Response<JsonBeanData> response) {
        LogUtils.e(response.body() + "");
        JsonBeanData bean = response.body();
        if (bean != null) {
            if (101 == bean.getCode()) {
                Toast.makeText(MyApp.getContext(), "token 过期，重新登录", Toast.LENGTH_SHORT).show();
                SPUtils.put(MyApp.getContext(), "userType", false);
                Intent i = new Intent(MyApp.getContext(), LoginActivity.class);
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                MyApp.getContext().startActivity(i);
            } else {
                onSuccess(bean.getCode(), bean.getMsg(), bean.getData());
            }
        } else {
            LogUtils.e("服务器返回值异常--->bean = null");
        }
    }

    @Override
    public void onError(Response<JsonBeanData> response) {
        LogUtils.e(response.body() + "");
        Throwable t = response.getException();
        LogUtils.e("网络请求错误---->" + t.getClass() + " : " + t.getMessage());
        if (t instanceof SocketTimeoutException || t instanceof ConnectException || t instanceof UnknownHostException || t instanceof UnknownServiceException || t instanceof SocketException) {
            Toast.makeText(MyApp.getContext(), R.string.load_failure, Toast.LENGTH_SHORT).show();
        }
        if (showLoadingDialog() && mLoadingDialog != null) {
            mLoadingDialog.dismiss();
        }
        onError();
    }

    public void onError() {

    }


    public abstract void onSuccess(int code, String msg, String info);

    @Override
    public void onStart(Request<JsonBeanData, ? extends Request> request) {
        onStart();
    }

    public void onStart() {
        if (showLoadingDialog()) {
            if (mLoadingDialog == null) {
                mLoadingDialog = createLoadingDialog();
            }
            mLoadingDialog.show();
        }
    }

    @Override
    public void onFinish() {
        if (showLoadingDialog() && mLoadingDialog != null) {
            mLoadingDialog.dismiss();
        }
    }

    public Dialog createLoadingDialog() {
        return null;
    }

    public boolean showLoadingDialog() {
        return false;
    }

}
